package com.cysyz.mylib.utils;

import java.lang.reflect.Field;

public abstract class MyBeanUtils {
	public static void printBeanInfo(Object bean, String... fieldNames) {
		Field field = null;
		if (fieldNames.length == 0) {
			for (Field tField : bean.getClass().getDeclaredFields()) {
				printField(bean, tField);
			}
		} else {
			
			for (String name : fieldNames) {
				try {
					field = bean.getClass().getDeclaredField(name);
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				}
				printField(bean, field);
			}

		}
	}

	private static void printField(Object bean, Field field) {
		StringBuffer fieldInfo;
		fieldInfo = new StringBuffer(field.getName());
		fieldInfo.append("[");
		fieldInfo.append(field.getType());
		fieldInfo.append("]");
		fieldInfo.append(":");
		try {
			if (!field.isAccessible()) {
				field.setAccessible(true);
			}
			fieldInfo.append(field.get(bean));
			System.out.println(fieldInfo);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
}
